package com.itlong.cloud.property;


import com.itlong.cloud.POJO.DTO.property.ParkPlaceProjectInfoDTO;
import com.itlong.cloud.common.network.tcp.netty.INettyServiceClient;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.ParkSystemTypeEnum;
import com.itlong.cloud.enumerate.ParkingBusinessOrderEnum;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.user.property.IPropertyParkingPassRecordServiceClient;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <desc>
 *      物业停车场同步记录指令TimeTask
 * </desc>
 *
 * @createDate 2018/11/08
 */
@EnableScheduling // 启用定时任务
@RestController
public class PropertyParkingRecordSychronizeTimedTask {
    private static Logger LOG = LoggerFactory.getLogger(PropertyParkingRecordSychronizeTimedTask.class);

    @Autowired
    IPropertyParkingPassRecordServiceClient iPropertyParkingPassRecordServiceClient;

    @Autowired
    INettyServiceClient iNettyServiceClient;

    @Autowired
    RedisService redisService;
    /**
     * <desc>
     *      定时任务发送获取车辆入场记录指令
     * </desc>
     *
     * @throws Exception
     * @author Jiaqi.X
     * @createDate 2018/08/14
     */
    @Scheduled(cron = "0/10 * * * * ?") // 每天凌晨执行
    public void sendInRecordInstruct() throws Exception {
        try {
            sendInOrOutInstruct(1);
        } catch (Exception e) {
            LOG.error("【定时任务】定时任务发送获取车辆入场记录指令失败");
            throw e;
        }
    }

    /**
     * <desc>
     *     发送入场或出场指令
     * </desc>
     *
     * @param type 指令类型 1入场 2出场
     * @author Jiaqi.X
     * @createDate 2018/11/08
     */
    private void sendInOrOutInstruct(int type) throws IOException {
        List<ParkPlaceProjectInfoDTO> projects = iPropertyParkingPassRecordServiceClient.getInStatusProjectIds();
        for(ParkPlaceProjectInfoDTO project : projects){
            try{
                String projectKey = iPropertyParkingPassRecordServiceClient.getParkingLotKey(project.getProjectId());
                StringBuilder params = new StringBuilder("");
                if(project.getSystemType() == ParkSystemTypeEnum.FUJI.getType()) {
                    //富士停车场
                    List<String> parkingIds = iPropertyParkingPassRecordServiceClient.getInStatusParkingIdByProjectId(project.getProjectId());
                    if (!parkingIds.isEmpty()) {
                        params.append("\"parkingId");
                        if (type == 1) {
                            params.append(" not in");
                        } else {
                            params.append(" in");
                        }
                        params.append(" (");
                        for (int i = 0; i < parkingIds.size(); i++) {
                            if (i != 0) {
                                params.append(" ,");
                            }
                            params.append(String.format(" '%s'", parkingIds.get(i)));
                            if (i == parkingIds.size() - 1) {
                                params.append(" )\"");
                            }
                        }
                    } else {
                        params.append("\"id > 0\"");
                    }
                }else if(project.getSystemType() == ParkSystemTypeEnum.REDDOOR.getType()){
                    //红门停车场
                    List<String> plantNos = iPropertyParkingPassRecordServiceClient.getInPlantNoByProjectId(project.getProjectId());
                    params.append(StringUtils.join(plantNos,","));
                }
                if (type == 1) {
                    iNettyServiceClient.sendQueryInOrOutRecord(projectKey, params.toString(),
                            ParkingBusinessOrderEnum.QUERY_IN_RECORD,project.getProjectId());
                } else {
                    iNettyServiceClient.sendQueryInOrOutRecord(projectKey, params.toString(),
                            ParkingBusinessOrderEnum.QUERY_OUT_RECORD,project.getProjectId());
                }
            }catch(Exception e){
                LOG.info(project.getProjectId()+"项目未开通");
            }
        }
    }

    /**
     * <desc>
     *      定时任务发送获取车辆入场记录指令
     * </desc>
     *
     * @throws Exception
     * @author Jiaqi.X
     * @createDate 2018/08/14
     */
    @Scheduled(cron = "0/10 * * * * ?") // 每天凌晨执行
    public void sendOutRecordInstruct() throws Exception {
        try {
            sendInOrOutInstruct(2);
        } catch (Exception e) {
            LOG.error("【定时任务】定时任务发送获取车辆出场记录指令失败");
            throw e;
        }
    }
}